package com.skywolf.chem.match.controller;


import com.skywolf.chem.common.sysconfig.exception.BizException;
import com.skywolf.chem.common.sysconfig.pojo.vo.PageVO;
import com.skywolf.chem.common.sysconfig.response.RespData;
import com.skywolf.chem.match.common.enums.MatchPayTypeEnum;
import com.skywolf.chem.match.params.param.MatchOrderConfirmParam;
import com.skywolf.chem.match.params.vo.*;
import com.skywolf.chem.match.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.PrintWriter;
import java.util.List;

/**
 * <p>
 * 订单详情表 前端控制器
 * </p>
 *
 * @author sj
 * @since 2021-09-24
 */
@AllArgsConstructor
@RestController
@RequestMapping("/match/order")
@Api(value = "赛事：订单管理", tags = {"赛事：订单管理API"})
public class MatchOrderController extends BaseController {

    private final IOrderService iOrderService;


    @ApiOperation(value = "订单确认", notes = "订单确认")
    @PostMapping(value = "/confirm")
    public RespData<OrderConfirmVO> confirm(@RequestBody @Validated MatchOrderConfirmParam param) {
        OrderConfirmVO vo = iOrderService.confirm(param, getUserId());
        return RespData.success(vo);

    }

    @ApiOperation(value = "订单提交", notes = "订单提交")
    @PostMapping(value = "/submit")
    public RespData<OrderSubmitVO> submit(@Validated @RequestBody OrderConfirmVO param) {
        OrderSubmitVO vo = iOrderService.submit(param, getUserId());
        return RespData.success(vo);
    }

    @ApiOperation(value = "订单支付", notes = "订单支付")
    @PostMapping(value = "/pay/{orderId}/{payType}/{source}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单编号", paramType = "path", dataType = "Long"),
            @ApiImplicitParam(name = "payType", value = "支付类型 1为微信支付 2为支付宝支付", paramType = "path", dataType = "Integer"),
            @ApiImplicitParam(name = "source", value = "设备类型 ： iOS, Android", paramType = "path", dataType = "String")
    })
    public RespData<OrderPayVo> pay(@PathVariable("orderId") Long orderId, @PathVariable("payType") Integer payType, @PathVariable("source") String source) throws Exception {
        MatchPayTypeEnum payTypeEnum = MatchPayTypeEnum.getByCode(payType);
        if (payTypeEnum == null) {
            throw new BizException("系统暂不支持该支付方式");
        }
        return RespData.success(iOrderService.pay(getUserId(), orderId, payTypeEnum, source));
    }

    @ApiOperation(value = "根据状态获取订单列表", notes = "根据状态获取订单列表")
    @GetMapping(value = "/get/list/by/{status}")
    @ApiImplicitParam(name = "status", value = "订单状态：全部->>0  待支付->>101  关注的活动->>201", paramType = "path", dataType = "Integer")
    public RespData<List<OrderListQueryVo>> getOrderListByStatus(@PathVariable("status") Integer status) throws Exception {
        return RespData.success(iOrderService.getOrderListByStatus(status, getUserId()));
    }

    @ApiOperation(value = "获取订单详情", notes = "获取订单详情")
    @GetMapping(value = "/get/info/by/{id}")
    @ApiImplicitParam(name = "id", value = "订单编号", paramType = "path", dataType = "Long")
    public RespData<OrderInfoQueryVo> getOrderInfo(@PathVariable("id") Long id) throws Exception {
        return RespData.success(iOrderService.orderInfoQueryVo(id));
    }

    @ApiOperation(value = "取消报名", notes = "取消报名")
    @GetMapping(value = "/cancel/{id}")
    @ApiImplicitParam(name = "id", value = "报名人编号", paramType = "path", dataType = "Long")
    public RespData cancelApply(@PathVariable("id") Long id) {
        iOrderService.cancelApply(id);
        return RespData.success();
    }


    @ApiOperation(value = "取消订单", notes = "取消订单")
    @GetMapping(value = "/cancel/order/{orderId}")
    @ApiImplicitParam(name = "id", value = "订单编号", paramType = "path", dataType = "Long")
    public RespData cancelOrder(@PathVariable("orderId") Long orderId) {
        iOrderService.cancelOrder(orderId);
        return RespData.success();
    }


}
